﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Bean;
using Shpm.App.Input.Dao;
using Shpm.App.Master.Bean;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Commons.Utils;
using System.Reflection;
using Shpm.System.Web.Control;
using log4net;
using Shpm.App.Input.Bean;

public partial class App_Input_PersonInfoForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Context.Items["PERSON_ID"]!=null)
            {
                ViewState["PERSON_ID"] = Context.Items["PERSON_ID"];
                
            }
            else
            {
                ViewState["PERSON_ID"] = null;
            }
            if (Context.Items["EMPLOYEE_ID"]!=null)
            {
                ViewState["EMPLOYEE_ID"] = Context.Items["EMPLOYEE_ID"];
                this.employeeId.Text = Convert.ToString(ViewState["EMPLOYEE_ID"]);
                this.employeeId.ReadOnly = true;

            }
            else
            {
                ViewState["EMPLOYEE_ID"] = null;
                this.employeeId.ReadOnly = false;
            }
            // 接收传递参数为分页编号
            if (Context.Items.Contains("ParamPageIndex"))
            {
                ViewState["ParamPageIndex"] = Convert.ToString(Context.Items["ParamPageIndex"]);
            }
            // 接收传递参数为父页编号
            if (Context.Items.Contains("URL"))
            {
                ViewState["URL"] = Convert.ToString(Context.Items["URL"]);
            }
            if (Context.Items.Contains("CMB_GROUP_ID"))
            {
                this.ViewState["CMB_GROUP_ID"] = Context.Items["CMB_GROUP_ID"];
            }
            if (Context.Items.Contains("CMB_JOB_ID"))
            {
                this.ViewState["CMB_JOB_ID"] = Context.Items["CMB_JOB_ID"];
            }
            if (Context.Items.Contains("CMB_STATUS_ID"))
            {
                this.ViewState["CMB_STATUS_ID"] = Context.Items["CMB_STATUS_ID"];
            }
            if (Context.Items.Contains("EMPLOYEE_STATUS"))
            {
                this.ViewState["EMPLOYEE_STATUS"] = Context.Items["EMPLOYEE_STATUS"];
            }
            if (Context.Items.Contains("CMB_EMPLOYEE_ID"))
            {
                this.ViewState["CMB_EMPLOYEE_ID"] = Context.Items["CMB_EMPLOYEE_ID"];
            }
            if (Context.Items.Contains("CMB_EMPLOYEE_NAME"))
            {
                this.ViewState["CMB_EMPLOYEE_NAME"] = Context.Items["CMB_EMPLOYEE_NAME"];
            }

            initDic();
            //ViewState["PERSON_ID"] = 169;
            dataBound();
            if (Convert.ToString(ViewState["EMPLOYEE_STATUS"]) == "-1")
            {
                btnAdd.Enabled = false;
            }
            else
            {
                btnAdd.Enabled = true;
            }
        }
        
    }
    void Page_PreRender(object sender, EventArgs e)
    {
        if (ViewState["EMPLOYEE_ID"] == null || Convert.ToString(ViewState["EMPLOYEE_STATUS"]) == "-1")
        {
            btnJob.Enabled = false;
        }
        else
        {
            btnJob.Enabled = true;
        }
        dataBound();
    }
    #region 设置标签
    protected void lnkTab_Command(object sender, CommandEventArgs e)
    {
        string directUrl = "~/App/Input/SaleProcesseMembershipForm.aspx";
        switch (e.CommandName)
        {
            case "btn0":
                // 基本信息
                // this.LinkButton1.CssClass = "selectli";
                // this.LinkButton2.CssClass = "";
                directUrl = "~/App/Input/PersonInfoForm.aspx";
                break;
            case "btn1":
                // 学历
                directUrl = "~/App/Input/PersonInfoEdu.aspx";
                break;
            case "btn3":
                // 工作经历
                directUrl = "~/App/Input/PersonInfoWork.aspx";
                break;
            case "btn2":
                // 家庭情况
                directUrl = "~/App/Input/PersonInfoFamily.aspx";
                break;
            case "btn4":
                // 福利
                directUrl = "~/App/Input/PersonInfoWelfare.aspx";
                break;
            case "btn5":
                //薪酬
                directUrl = "~/App/Input/PersonInfoSalaryForm.aspx";
                break;
            case "btn6":
                //奖惩
                directUrl = "~/App/Input/PersonBonusPenaltyForm.aspx";
                break;
            case "btn7":
                //报道
                directUrl = "~/App/Input/PersonPrepareForm.aspx";
                break;
            case "btn8":
                //培训
                directUrl = "~/App/Input/PersonTrainForm.aspx";
                break;
            default:
                break;
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["PERSON_ID"])))
        {
            Context.Items["PERSON_ID"] = this.ViewState["PERSON_ID"];
            Context.Items["EMPLOYEE_ID"] = this.ViewState["EMPLOYEE_ID"];
            Context.Items["CMB_JOB_ID"] = ViewState["CMB_JOB_ID"];
            Context.Items["CMB_GROUP_ID"] = ViewState["CMB_GROUP_ID"];
            Context.Items["CMB_STATUS_ID"] = ViewState["CMB_STATUS_ID"];
            Context.Items["ParamPageIndex"] = ViewState["ParamPageIndex"];
            Context.Items["URL"] = ViewState["URL"];
            Context.Items["EMPLOYEE_STATUS"] = ViewState["EMPLOYEE_STATUS"];
            ControllerUtils.Transfer(this.Context, directUrl, false);
        }
        else
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0035").ToString();
        }
        
    }
    #endregion

    /*
     * 函数名：initDic
     * 功能：控件数据绑定
     * 参数：无
     * 说明：将数据字典中的数据绑定在指定的控件上
     */
    private void initDic()
    {

        CommonDictionaryBean paramBean = new CommonDictionaryBean();
        CommonDictionaryDao dao = new CommonDictionaryDao();
        /*籍贯*/
        paramBean.Catalog = "M_PERSON.NATIVE";
        initAll(dao, paramBean, this.native);
        /*户籍性质*/
        paramBean.Catalog = "M_PERSON.REGISTER_TYPE";
        initAll(dao, paramBean, this.rblRegisterType);
        if (rblRegisterType.Items.Count > 1)
            rblRegisterType.SelectedIndex = 0;
        /*血型*/
        paramBean.Catalog = "BLOOD_TYPE";
        initAll(dao, paramBean, this.blood);
        
    }

    /*
    * 函数名：initAll
    * 功能  ：控件数据绑定
    * 参数  ：dao(数据字典的DAO）
    *       ：paramBean(数据字典的Bean）
    *       ：obj（控件）
    * 说明  ：
    */
    private void initAll(CommonDictionaryDao dao, CommonDictionaryBean paramBean, ListControl obj)
    {
        IList list = null;
        list = dao.FindByCatalog(paramBean);

        obj.DataSource = list;
        obj.DataBind();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
     {

        if (this.cardType.SelectedIndex == 0)
        {
            ComAbsParamsAttribute param1 = new ComAbsParamsAttribute();
            param1.Parameter = "身份证号";
            ComAbsParamsAttribute param2 = new ComAbsParamsAttribute();
            param2.Parameter = "15";
            ComAbsParamsAttribute param3 = new ComAbsParamsAttribute();
            param3.Parameter = "18";



            CheckMinByteLenAttribute atr = new CheckMinByteLenAttribute();
            CheckMaxByteLenAttribute atr2 = new CheckMaxByteLenAttribute();

            atr.ByteLen = 15;
            atr2.ByteLen = 18;

            atr.Parameters.Add(param1);
            atr.Parameters.Add(param2);
            atr.Parameters.Add(param3);

            atr2.Parameters.Add(param1);
            atr2.Parameters.Add(param2);
            atr2.Parameters.Add(param3);

            atr.Code = "E0316";
            atr.ControlToValidator = "card";
            atr2.Code = "E0316";
            atr2.ControlToValidator = "card";
            ComValidator1.CheckItems.Add(atr);
            ComValidator1.CheckItems.Add(atr2);
        }
        else
        {
            ComAbsParamsAttribute param1 = new ComAbsParamsAttribute();
            param1.Parameter = "护照";
            ComAbsParamsAttribute param2 = new ComAbsParamsAttribute();
            param2.Parameter = "9";
            CheckMinByteLenAttribute atr = new CheckMinByteLenAttribute();
            CheckMaxByteLenAttribute atr2 = new CheckMaxByteLenAttribute();
            atr.ByteLen = 9;
            atr2.ByteLen = 9;
            atr.Parameters.Add(param1);
            atr.Parameters.Add(param2);
            atr2.Parameters.Add(param1);
            atr2.Parameters.Add(param2);
            atr.Code = "E0319";
            atr.ControlToValidator = "card";
            atr2.Code = "E0319";
            atr2.ControlToValidator = "card";
            ComValidator1.CheckItems.Add(atr);
            ComValidator1.CheckItems.Add(atr2);
        }
        if (this.ComValidator1.DoValidator() == false)
        {

            ((Design_MasterPage)this.Master).InfoMessage = this.ComValidator1.ErrorMessage;

            return;
        }
        SaveBase();

    }
    /*
     * 函数名：dataBound
     * 功能：读取数据并绑定在控件上
     * 参数：无
     * 说明：
     */
    private void dataBound()
    {
        if (this.ViewState["PERSON_ID"] != null)
        {
            SaleProcessMemberShipBean dataBean = new SaleProcessMemberShipBean();
            SaleProcessMemberShipBean newBean = new SaleProcessMemberShipBean();
            PersonDao Dao = new PersonDao();
            dataBean.PersonId = Convert.ToInt32(ViewState["PERSON_ID"]);
            this.personId.Value = Convert.ToString(dataBean.PersonId);
            newBean = Dao.GetEnployee(dataBean);
            if (newBean != null)
            {
                nameC.Text = newBean.NameC;
                this.sex.SelectedValue = (this.sex.Items.FindByValue(newBean.Sex) == null) ? null : newBean.Sex;
                this.birthday.Text = newBean.Birthday == null ? "" : DateUtils.convertToString((DateTime)newBean.Birthday);//newBean.Birthday.ToString().Split(' ')[0];
                this.blood.SelectedValue = (this.native.Items.FindByValue(newBean.BloodType) == null) ? null : newBean.BloodType;
                this.nameE.Text = newBean.NameE;
                this.card.Text = newBean.Card;
                this.cardType.SelectedValue = (this.cardType.Items.FindByValue(newBean.CardType) == null) ? null : newBean.CardType;
                this.txtXueli.Text = newBean.Xueli;
                this.txtFamily.Text = newBean.Family;
                this.marriage.SelectedValue = (this.marriage.Items.FindByValue(newBean.Marriage) == null) ? null : newBean.Marriage;
                this.native.SelectedValue = (this.native.Items.FindByValue(newBean.Native) == null) ? null : newBean.Native;
                this.nationality.Text = newBean.Nationality;
                this.addressRegister.Text = newBean.AddressRegister;
                this.rblRegisterType.SelectedValue = (this.rblRegisterType.Items.FindByValue(newBean.RegisterType) == null) ? null : newBean.RegisterType;
                this.addressHome.Text = newBean.AddressHome;
                this.txtUrgencyRelation.Text = newBean.UrgencyRelation;
                this.txtEvaluation.Text = newBean.Evaluation;
                this.txtHealth.Text = newBean.Health;
                this.tel.Text = newBean.Tel;
                this.handSet.Text = newBean.HandSet;
                this.txtUrgencyTel.Text = newBean.UrgencyTel;
                this.Image1.ImageUrl = "~/App/Input/PersonPhoto.aspx?ID=" + dataBean.PersonId;
                
            }
        }
        if (ViewState["EMPLOYEE_ID"] != null)
        {
            PersonEmployeeBean dataBean1 = new PersonEmployeeBean();
            PersonEmployeeBean bean = new PersonEmployeeBean();
            PersonEmployeeDao Dao1 = new PersonEmployeeDao();
            dataBean1.EmployeeId = this.employeeId.Text;
            dataBean1=Dao1.GetEnployeeId(dataBean1);
            if (dataBean1 != null)
            {
                this.employeeDate.Text = dataBean1.EmployeeDate == null ? "" : DateUtils.convertToString((DateTime)dataBean1.EmployeeDate);
            }
            bean.EmployeeId = Convert.ToString(ViewState["EMPLOYEE_ID"]);
            bean=Dao1.Find(bean);
            if (bean != null)
            {
                group.Text = bean.GroupName;
                job.Text = bean.JobName;
                status.Text = bean.StatusName;
            }
        }
    }

    /*
     * 函数名：SaveBase
     * 功能：数据保存
     * 参数：无
     * 说明：
     */
    private void  SaveBase()
    {

        SysUserManager manager = new SysUserManager(Context);
        string userId = manager.getSysUserBean().getMstSysUserBean().SysUserId;
        SaleProcessMemberShipBean dataBean = new SaleProcessMemberShipBean();
        PersonDao dao = new PersonDao();
        dataBean.NameC = nameC.Text;
        dataBean.NameE = nameE.Text;
        dataBean.Sex = sex.SelectedValue;
        dataBean.Birthday =  DateUtils.convertToDate(this.birthday.Text);//(this.birthday.Text.Length>0)?DateUtils.convertToDate(this.birthday.Text):null;
        dataBean.Marriage = this.marriage.SelectedValue;
        dataBean.Card = card.Text;
        dataBean.CardType = cardType.SelectedValue;
        dataBean.AddressRegister = this.addressRegister.Text;
        dataBean.AddressHome = this.addressHome.Text;
        dataBean.Tel = this.tel.Text;
        dataBean.HandSet = this.handSet.Text;
        dataBean.Xueli = txtXueli.Text;
        dataBean.Family = txtFamily.Text;
        dataBean.BloodType = blood.SelectedValue;
        dataBean.Native = ("".Equals(this.native.Text)) ? null : this.native.SelectedValue;
        dataBean.Nationality = nationality.Text;
        dataBean.RegisterType = rblRegisterType.SelectedValue;
        dataBean.UrgencyRelation = txtUrgencyRelation.Text;
        dataBean.UrgencyTel = txtUrgencyTel.Text;
        dataBean.Evaluation = txtEvaluation.Text;
        dataBean.Health = txtHealth.Text;
        dataBean.UserId = userId;
        if (this.FileUpload1.FileBytes.LongLength > 0)
        {
            dataBean.Photo = FileUpload1.FileBytes;
        }
        if (this.ViewState["PERSON_ID"] != null)
        {

            PersonEmployeeBean bean = new PersonEmployeeBean();
            PersonEmployeeDao Dao1 = new PersonEmployeeDao();
            dataBean.PersonId = Convert.ToInt32(ViewState["PERSON_ID"]);
            dao.UpdEmployee(dataBean);
            bean.EmployeeId = this.employeeId.Text;
            bean.EmployeeName = this.nameC.Text;
            bean.EmployeeDate = DateUtils.convertToDate(this.employeeDate.Text);
            bean.UserId = userId;
            Dao1.UpdEmployee(bean);
            if (dataBean.Photo != null)
            {
                dao.UpdPhoto(dataBean);
            }
            ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();
        }
        else
        {
            PersonEmployeeBean dataBean1 = new PersonEmployeeBean();
            PersonEmployeeBean bean = new PersonEmployeeBean();
            PersonEmployeeDao Dao1 = new PersonEmployeeDao();
            dataBean1.EmployeeId = this.employeeId.Text;
            if (Dao1.GetEnployeeId(dataBean1) != null)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0038").ToString();
                return;
            }
            ViewState["PERSON_ID"] = (int)dao.InsertEmployee(dataBean);
            bean.PersonId = Convert.ToInt32(ViewState["PERSON_ID"]);
            bean.EmployeeId = this.employeeId.Text;
            bean.EmployeeDate = DateUtils.convertToDate(this.employeeDate.Text);
            bean.EmployeeName = this.nameC.Text;
            bean.UserId = userId;
            Dao1.InsertEmployeeId(bean);
            this.employeeId.ReadOnly = true;
            ViewState["EMPLOYEE_ID"] = this.employeeId.Text;
            if (dataBean.Photo != null)
            {
                dao.UpdPhoto(dataBean);
            }
            ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        // 传递分页编号
        Context.Items["ParamPageIndex"] = ViewState["ParamPageIndex"];
        Context.Items["CMB_GROUP_ID"] = Convert.ToString(this.ViewState["CMB_GROUP_ID"]);
        Context.Items["CMB_JOB_ID"] = Convert.ToString(this.ViewState["CMB_JOB_ID"]);
        Context.Items["CMB_STATUS_ID"] = Convert.ToString(this.ViewState["CMB_STATUS_ID"]);
        Context.Items["CMB_EMPLOYEE_ID"] = Convert.ToString(this.ViewState["CMB_EMPLOYEE_ID"]);
        Context.Items["CMB_EMPLOYEE_NAME"] = Convert.ToString(this.ViewState["CMB_EMPLOYEE_NAME"]);
        //string url = Convert.ToString(ViewState["URL"]);
        string url = "~/App/Input/PersonEmployeeSrhForm.aspx";
        ControllerUtils.Transfer(Context, url, true);    
    }
    protected void btnJob_Click(object sender, EventArgs e)
    {
        string url = "~/App/Input/PersonEmployeeJobForm.aspx";
        // 传递分页编号
        Context.Items["EMPLOYEE_ID"] = Convert.ToString(this.ViewState["EMPLOYEE_ID"]);
        Context.Items["URL"] = "~/App/Input/PersonInfoForm.aspx";
        Context.Items["ParamPageIndex"] = ViewState["ParamPageIndex"];
        Context.Items["CMB_GROUP_ID"] = Convert.ToString(this.ViewState["CMB_GROUP_ID"]);
        Context.Items["CMB_JOB_ID"] = Convert.ToString(this.ViewState["CMB_JOB_ID"]);
        Context.Items["CMB_STATUS_ID"] = Convert.ToString(this.ViewState["CMB_STATUS_ID"]);
        Context.Items["CMB_EMPLOYEE_ID"] = Convert.ToString(this.ViewState["CMB_EMPLOYEE_ID"]);
        Context.Items["CMB_EMPLOYEE_NAME"] = Convert.ToString(this.ViewState["CMB_EMPLOYEE_NAME"]);
        ControllerUtils.Transfer(Context, url, true);
    }
}
